package com.cbb.display.service.impl;

import com.cbb.display.entity.SysTask;
import com.cbb.display.config.quartz.QuartzManager;
import com.cbb.display.dao.SysTaskMapper;
import com.cbb.display.service.SysTaskService;
import com.cbb.display.enums.JobEnum;
import com.cbb.display.enums.JobStatusEnum;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bigbomb
 * @since 2020-01-18
 */
@Service
public class SysTaskServiceImpl extends ServiceImpl<SysTaskMapper, SysTask> implements SysTaskService {

	  @Autowired
	  private SysTaskMapper taskScheduleJobMapper;
	 
	  @Autowired
	  QuartzManager quartzManager;
	  
	  @Resource
	  private CacheManager cacheManager;
	  @Override
	  public SysTask get(Long id) {
	    return taskScheduleJobMapper.selectById(id);
	  }
	  
	 
	  @Override
		public int addJob(SysTask scheduleJob) {
			if (scheduleJob == null) {
			    return 0;
			}
     
        quartzManager.addJob(scheduleJob);
        return add(scheduleJob);	    
	  }
	  
	  
	  
	@Override
	public int remove(Long id) {
	try {
		SysTask scheduleJob = get(id);
		quartzManager.deleteJob(scheduleJob);
		return taskScheduleJobMapper.deleteById(id);
		} catch (SchedulerException e) {
			e.printStackTrace();
			return 0;
        }
	}
	
	
	@Override
	public int batchRemove(Long[] ids) {
		// TODO Auto-generated method stub
		for (Long id : ids) {
		try {
			SysTask scheduleJob = get(id);
			quartzManager.deleteJob(scheduleJob);
			} catch (SchedulerException e) {
					e.printStackTrace();
					return 0;
			}
      }
    return taskScheduleJobMapper.deleteBatchIds(Arrays.asList(ids));

		
	}

	@Override
	public void initSchedule() {
		// TODO Auto-generated method stub
		QueryWrapper<SysTask> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("jobStatus", "1");
	    List<SysTask> jobList = taskScheduleJobMapper.selectList(queryWrapper);
        for (SysTask scheduleJob : jobList) {
             quartzManager.addJob(scheduleJob);
		}
        List<Map<String, Object>> jobEnumList = new ArrayList<Map<String, Object>>();
        for (JobEnum jobEnum : JobEnum.values()) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put( jobEnum.getCode(),jobEnum.getValue());
            jobEnumList.add(map);
        }
        Cache cache = cacheManager.getCache("cache");
        cache.put("jobEnumList", jobEnumList);

	}

	@Override
	public int changeStatus(Long jobId, String cmd) {
		try {
			SysTask scheduleJob = get(jobId);
			if (scheduleJob == null) {
			    return 0;
	        }
	        if (JobStatusEnum.RUNNING.getCode().equals(cmd)) {
	            quartzManager.pauseJob(scheduleJob);
	            scheduleJob.setJobStatus(JobStatusEnum.STOP.getCode());
			}  else {
				scheduleJob.setJobStatus(JobStatusEnum.RUNNING.getCode());
				quartzManager.resumeJob(scheduleJob);
			}
         return update(scheduleJob);
		}catch (SchedulerException e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
		
		    
  }


	@Override
	public int updateCron(Long jobId,String cronString) throws SchedulerException {
		// TODO Auto-generated method stub
		SysTask scheduleJob = get(jobId);
		scheduleJob.setCronExpression(cronString);
		if (scheduleJob == null) {
		    return 0;
        }
		else if(!scheduleJob.getJobStatus().equals(JobStatusEnum.STOP.getCode()))
		{
			quartzManager.updateJobCron(scheduleJob);
		}
          return update(scheduleJob);
	}

	@Override
	public int update(SysTask taskScheduleJob) {
		// TODO Auto-generated method stub
		return taskScheduleJobMapper.updateById(taskScheduleJob);
		
	}
	
	public int add(SysTask taskScheduleJob)
	{
		return taskScheduleJobMapper.insert(taskScheduleJob);
	}
}

